#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::Cisco::ShowProcessMemory < Aio::Module::Cmd::Cisco
  include Aio::Module

  def initialize
    super({
      :cmd_full 	=> "show process memory",
      :cmd_short	=> "sh pro m",
      :author			=> "Elin",
      :description	=> "This is Cisco Command# show process memory",
      :ranking		=> Ranking_1,
      :platform		=> "all",
      :benchmark	=> {
        :proc_ratio	=> ["<", 70.0],
        :io_ratio		=> ["<", 70.0]
      }
    })
  end

  def parse
    cont = self.context.dup
    useful[:memory] = {}

    case device_template
    when Template_1
      parse_def(cont)
    when Template_2
      if device_version =~ /12.2\(33\)/ # 当是Cisco7606 的时候，即使是12.2(17r)S4，Memory的模板也是默认模板
        parse_def(cont)
      else
        parse_tmp_2(cont)
      end
    end
  end

  def parse_tmp_2(cont)
    cont.readline_match_block(/System Memory : (?<proc_total>\d+)K total, (?<proc_used>\d+)K used/) do |block|
      proc_ratio = ratio(block[:proc_used], block[:proc_total])
      block.warning_serious(useful[:memory], :proc_ratio, self, {:string => proc_ratio})
    end
  end

  def parse_def(cont)
    cont.readline_match_block(/Processor Pool Total: (?<proc_total>\d+) Used: (?<proc_used>\d+)/) do |block|
      proc_ratio = ratio(block[:proc_used], block[:proc_total])
      block.warning_serious(useful[:memory], :proc_ratio, self, {:string => proc_ratio})
    end
    cont.readline_match_block(/I\/O Pool Total: (?<io_total>\d+) Used: (?<io_used>\d+)/) do |block|
      io_ratio = ratio(block[:io_used], block[:io_total])
      #block.warning_serious(useful[:memory], :io_ratio, self, io_ratio)
      block.update(useful[:memory], :io_ratio, io_ratio)
    end
  end

  # 返回为String
  def ratio(fir, sec)
    format("%.2f", ((fir.to_f / sec.to_f) * 100))
  end
end

